import { createRouter, createWebHistory } from 'vue-router'
import { useMainStore } from '../store'

// 子路由配置
const childRoutes = [
  {
    path: "",
    name: "index",
    component: () => import('../components/index.vue'),
    meta: { layout: 'default', title: '首页' }
  },
  {
    path: "sort",
    name: "sort",
    component: () => import('../components/sort.vue'),
    meta: { layout: 'default', title: '分类' }
  },
  {
    path: "article",
    name: "article",
    component: () => import('../components/article.vue'),
    meta: { layout: 'default', title: '文章' }
  },
  {
    path: "weiYan",
    name: "weiYan",
    component: () => import('../components/weiYan.vue'),
    meta: { layout: 'default', title: '微言' }
  },
  {
    path: "message",
    name: "message",
    component: () => import('../components/message.vue'),
    meta: { layout: 'default', title: '留言' }
  },
  {
    path: "friend",
    name: "friend",
    component: () => import('../components/friend.vue'),
    meta: { layout: 'default', title: '友人帐' }
  },
  {
    path: "bangumi",
    name: "bangumi",
    component: () => import('../components/bangumi.vue'),
    meta: { layout: 'default', title: '追番' }
  },
  {
    path: "izone",
    name: "izone",
    component: () => import('../components/izone.vue'),
    meta: { layout: 'default', title: '自定义分区' }
  },
  {
    path: "album",
    name: "album",
    component: () => import('../components/album.vue'),
    meta: { layout: 'default', title: '相册' }
  },
  {
    path: "about",
    name: "about",
    component: () => import('../components/about.vue'),
    meta: { layout: 'default', title: '关于' }
  },
  {
    path: "user",
    name: "user",
    component: () => import('../components/user.vue'),
    meta: { layout: 'default', title: '用户' }
  },
  {
    path: "letter",
    name: "letter",
    component: () => import('../components/letter.vue'),
    meta: { layout: 'default', title: '信件' }
  },
  {
    path: "calendar",
    name: "calendar",
    component: () => import('../components/calendar.vue'),
    meta: { layout: 'default', title: '日历' }
  },
  {
    path: "favorite",
    name: "favorite",
    component: () => import('../components/favorite.vue'),
    meta: { layout: 'default', title: '收藏' }
  }
]

// 主路由配置
const routes = [
  {
    path: '/',
    component: () => import('../components/home.vue'),
    children: childRoutes
  },
  {
    // 微前端入口点 - 所有/admin开头的路径都由微应用处理
    path: '/admin/:pathMatch(.*)*',
    name: 'Admin',
    component: () => import('../views/AdminEntry.vue'),
    meta: { title: '管理后台' }
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../components/user.vue'),
    meta: { title: '登录' }
  },
  {
    path: '/verify',
    name: 'verify',
    component: () => import('../components/user.vue'),
    meta: { title: '验证' }
  }
]

// 创建路由实例
const router = createRouter({
  history: createWebHistory(),
  routes,
  scrollBehavior: (to, from, savedPosition) => {
    return { x: 0, y: 0 }
  }
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = to.meta.title + ' - 博客系统'
  }
  
  // 权限验证
  if (to.meta.requiresAuth) {
    const store = useMainStore()
    const isLoggedIn = store.currentUser && store.currentUser.id
    
    if (!isLoggedIn) {
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router
